﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;


using System.Threading;

namespace AT_Command_Tester
{
    public partial class Form1 : Form
    {
        #region Private Variables
        SerialPort port = new SerialPort();
        clsSMS objclsSMS = new clsSMS();
        ShortMessageCollection objShortMessageCollection = new ShortMessageCollection();
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //Set Port Name
                String portName = "Com6";
                Int32 boundRate = 115200;
                Int32 dataBitRate = 8;
                Int32 readTimeOut = 3000;
                Int32 writeTimeOut = 3000;

                //Open communication port 
                this.port = objclsSMS.OpenPort(portName, boundRate, dataBitRate, readTimeOut, writeTimeOut);

                if (this.port == null)
                {
                    lblerrorg.Text = "Invalid port settings";
                }
                else
                {
                    //ThreadPool.QueueUserWorkItem(new WaitCallback(ReadSMS), new object[] { "" });
                    //Thread.Sleep(1000);
                    ReadSMS("");
                }
            }
            catch (Exception ex)
            {
                lblerrorg.Text = ex.Message;
            }
        }


        private void ReadSMS(Object ParamValues)
        {
            try
            {
                //count SMS 
                int uCountSMS = objclsSMS.CountSMSmessages(this.port);
                if (uCountSMS > 0)
                {
                    string strCommand = "AT+CMGL=\"ALL\"";

                    // If SMS exist then read SMS
                    #region Read SMS
                    //.............................................. Read all SMS ....................................................
                    objShortMessageCollection = objclsSMS.ReadSMS(this.port, strCommand);
                    listboxRecevedSMS.Items.Clear();
                    foreach (ShortMessage msg in objShortMessageCollection)
                    {
                        listboxRecevedSMS.Items.Add("Message Index : " + msg.Index + "; Sender : " + msg.Sender + " <Massage> : " + msg.Message);
                    }
                    #endregion

                }
                else
                {
                    listboxRecevedSMS.Items.Clear();
                    //MessageBox.Show("There is no message in SIM");
                    listboxRecevedSMS.Items.Add("There is no message in SIM");

                }
            }
            catch (Exception ex)
            {
                lblerrorg.Text = ex.Message;
            }
        }




    }
}
